package game.leon.seawar.server.handler.impl;

import clojure.lang.RT;
import clojure.lang.Var;
import game.leon.engine.pattern.Fun;
import game.leon.seawar.server.handler.FunParam;
import game.leon.seawar.vo.BaseVO;
import game.leon.seawar.meta.Battle;
import game.leon.seawar.server.MessageReceiver;
import game.leon.seawar.server.service.BattleService;
import game.leon.seawar.vo.Vo;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * User: bianpeng
 * Date: 11-12-30
 */
@Component
public class CreateBattleFun implements Fun<FunParam<Vo.CreateBattleParamVO>, BaseVO> {
    private Logger logger = Logger.getLogger(getClass());
    @Autowired
    private BattleService battleService;
    @Autowired
    private MessageReceiver messageReceiver;
    @Override
    public BaseVO exec(FunParam<Vo.CreateBattleParamVO> data) {
        Var create = RT.var("seawar.battle", "battle-create");
        create.invoke();

        Var message = RT.var("seawar.battle", "battle-message");
        message.invoke();

        Vo.CreateBattleParamVO vo = data.getData();
        long striker = data.getSession().getUserId();
        long counter = vo.counter;
        Battle battle = battleService.createBattle(striker, counter);

        logger.info(String.format("battle created: %d vs %d",
                battle.getStriker(), battle.getCounter()));

        /*
        Vo.BattleTickVO tickVO = new Vo.BattleTickVO();
        tickVO.striker = striker;

        messageReceiver.delay(new DelayMessage(
                data.getSession(),
                EngineConsts.BATTLE_TICKS,
                tickVO,
                System.currentTimeMillis() + 500
        ));
        */
        return null;
    }
}
